#include "connectedwin.h"

#include <QVariant>
#include <QRgb>

const int HEIGHT = 164;
const int DEVICEICON_W = 96;
const int DEVICEICON_H = 96;
const int STATEICON_W = 8;
const int STATEICON_H = 8;
const int SCREENBTN_W = 104;
const int SCREENBTN_H = 48;

ConnectedWin::ConnectedWin(QWidget *parent) : QWidget(parent)
{
    // setAutoFillBackground(true);
    // QPalette palette = this->palette();
    // palette.setColor(QPalette::Base, QColor(98, 142, 255, 0.5));
    // this->setPalette(palette);
    // this->setAttribute(Qt::WA_WState_WindowOpacitySet);
    // setWindowOpacity(1);

    setAttribute(Qt::WA_StyledBackground);
    setObjectName("wid");
    setStyleSheet("QWidget#wid{background-color: rgba(98, 142, 255, 0.1);}");

    m_deviceIcon = new QLabel(this);
    m_deviceIcon->setFixedSize(DEVICEICON_W, DEVICEICON_H);
    m_deviceIcon->setPixmap(QIcon(":/devicetype/pc.svg").pixmap(QSize(DEVICEICON_W, DEVICEICON_H)));

    m_deviceName = new QLabel(this);
    QFont font;
    font.setPointSizeF(24);
    font.setBold(true);
    m_deviceName->setFont(font);
    m_deviceName->setText("Kylin");

    m_stateIcon = new QLabel(this);
    m_stateIcon->setFixedSize(STATEICON_W, STATEICON_H);
    m_stateIcon->setPixmap(QIcon(":/view/ellipse.svg").pixmap(QSize(STATEICON_W, STATEICON_H)));

    m_connectState = new QLabel(this);
    m_connectState->setText(tr("CONNECTED"));

    m_screenBtn = new QPushButton(this);
    connect(m_screenBtn, &QPushButton::clicked, this, &ConnectedWin::onClicked);
    m_screenBtn->setMinimumSize(SCREENBTN_W, SCREENBTN_H);
    m_screenBtn->setText("SCREEN");
    m_screenBtn->setProperty("isImportant", true);

    m_disconnectBtn = new QPushButton(this);
    connect(m_disconnectBtn, &QPushButton::clicked, this, &ConnectedWin::onClicked);
    m_disconnectBtn->setFixedSize(SCREENBTN_H, SCREENBTN_H);
    m_disconnectBtn->setIconSize(QSize(SCREENBTN_H, SCREENBTN_H));
    m_disconnectBtn->setIcon(QIcon(":/view/exit.svg"));
    m_disconnectBtn->setProperty("isWindowButton", 0x2);
    m_disconnectBtn->setProperty("useIconHighlightEffect", 0x8);
    m_disconnectBtn->setAutoFillBackground(true);
    m_disconnectBtn->setBackgroundRole(QPalette::Base);

    QHBoxLayout *hLayout = new QHBoxLayout;
    hLayout->setMargin(0);
    hLayout->setSpacing(0);
    hLayout->addWidget(m_stateIcon);
    hLayout->addSpacing(4);
    hLayout->addWidget(m_connectState);
    hLayout->addStretch();

    QVBoxLayout *vLayout = new QVBoxLayout;
    vLayout->setMargin(0);
    vLayout->setSpacing(0);
    vLayout->addStretch();
    vLayout->addWidget(m_deviceName);
    vLayout->addSpacing(5);
    vLayout->addLayout(hLayout);
    vLayout->addStretch();

    QHBoxLayout *layout = new QHBoxLayout;
    layout->setMargin(0);
    layout->setSpacing(0);
    layout->addSpacing(72);
    layout->addWidget(m_deviceIcon);
    layout->addSpacing(29);
    layout->addLayout(vLayout);
    layout->addStretch();
    layout->addWidget(m_screenBtn);
    layout->addSpacing(16);
    layout->addWidget(m_disconnectBtn);
    layout->addSpacing(72);
    QWidget *titleWin = new QWidget(this);
    titleWin->setFixedHeight(HEIGHT);
    titleWin->setLayout(layout);

    m_backgroundWin = new BackgroundWin(this);
    m_backgroundWin->setMinimumHeight(467);
    m_backgroundWin->setBackground(true);
    m_layout = new QHBoxLayout;
    m_layout->setMargin(0);
    m_layout->setSpacing(0);
    m_backgroundWin->setLayout(m_layout);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->setMargin(0);
    mainLayout->setSpacing(0);
    mainLayout->addWidget(titleWin);
    mainLayout->addWidget(m_backgroundWin);
    setLayout(mainLayout);
}

void ConnectedWin::setTheme(PublicAttributes::Theme theme)
{
    m_backgroundWin->setTheme(theme);
}

void ConnectedWin::setDeviceName(QString deviceName)
{
    m_deviceName->setText(deviceName);
}

void ConnectedWin::setDeviceType(PublicAttributes::DeviceType type)
{
    switch (type) {
    case PublicAttributes::DeviceType::Pc: {
        m_deviceIcon->setPixmap(QIcon(":/devicetype/pc.svg").pixmap(QSize(DEVICEICON_W, DEVICEICON_H)));
        m_screenBtn->setText(tr("ComputerScreen"));
    } break;

    case PublicAttributes::DeviceType::Android: {
        m_deviceIcon->setPixmap(QIcon(":/devicetype/phone.svg").pixmap(QSize(DEVICEICON_W, DEVICEICON_H)));
        m_screenBtn->setText(tr("MobileScreen"));
    } break;
    }
    m_ScreenText = m_screenBtn->text();
}

void ConnectedWin::addWidget(QWidget *win)
{
    m_layout->addWidget(win);
}

void ConnectedWin::restoreScreenButton()
{
    if (m_isScreenBtnClicked) {
        m_isScreenBtnClicked = false;
        m_screenBtn->setText(m_ScreenText);
    }
}

void ConnectedWin::changeScreenButton(bool isShow)
{
    if (isShow) {
        m_screenBtn->show();
    } else {
        m_screenBtn->hide();
    }
}

void ConnectedWin::onClicked()
{
    QPushButton *btn = qobject_cast<QPushButton *>(sender());
    if (btn == m_screenBtn) {
        if (m_isScreenBtnClicked) {
            m_isScreenBtnClicked = false;
            Q_EMIT sigBtnClicked(ConnectedWin::BtnType::ExitScreen);
            m_screenBtn->setText(m_ScreenText);
        } else {
            m_isScreenBtnClicked = true;
            Q_EMIT sigBtnClicked(ConnectedWin::BtnType::Screen);
            m_screenBtn->setText(tr("ExitScreen"));
        }
    } else if (btn == m_disconnectBtn) {
        Q_EMIT sigBtnClicked(ConnectedWin::BtnType::Disconnect);
    }
}
